package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.activity.datainput.DIBaseMorePicActivity;
import com.jiyi.bean.SingleImageBean;
import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireSpaceBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.inteface.DownloadPicOverInterface;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.base.AppMediaType;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MultipartBody;

/**
 * Created by Administrator on 2018/3/22.
 */

public class FireSpaceActivity extends DIBaseMorePicActivity implements DownloadPicOverInterface {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;


    @BindView(R.id.rbSettingYes)
    RadioButton rbSettingYes;
    @BindView(R.id.rbSettingNo)
    RadioButton rbSettingNo;
    @BindView(R.id.rgSettingIsOK)
    RadioGroup rgSettingIsOK;
    @BindView(R.id.etSettingProblem)
    EditText etSettingProblem;
    @BindView(R.id.etSettingMeasure)
    EditText etSettingMeasure;
    @BindView(R.id.rbMarkYes)
    RadioButton rbMarkYes;
    @BindView(R.id.rbMarkNo)
    RadioButton rbMarkNo;
    @BindView(R.id.rgMarkIsOK)
    RadioGroup rgMarkIsOK;
    @BindView(R.id.etMarkProblem)
    EditText etMarkProblem;
    @BindView(R.id.etMarkMeasure)
    EditText etMarkMeasure;
    @BindView(R.id.rbObstacleYes)
    RadioButton rbObstacleYes;
    @BindView(R.id.rbObstacleNo)
    RadioButton rbObstacleNo;
    @BindView(R.id.rgHasObstacle)
    RadioGroup rgHasObstacle;
    @BindView(R.id.etObstacleProblem)
    EditText etObstacleProblem;
    @BindView(R.id.etObstacleMeasure)
    EditText etObstacleMeasure;
    @BindView(R.id.rbBlockYes)
    RadioButton rbBlockYes;
    @BindView(R.id.rbBlockNo)
    RadioButton rbBlockNo;
    @BindView(R.id.rgHasBlock)
    RadioGroup rgHasBlock;
    @BindView(R.id.etBlockProblem)
    EditText etBlockProblem;
    @BindView(R.id.etBlockMeasure)
    EditText etBlockMeasure;


    @BindView(R.id.gvSetting)
    NoScrollGridView gvSetting;
    @BindView(R.id.gvMark)
    NoScrollGridView gvMark;
    @BindView(R.id.gvObstacle)
    NoScrollGridView gvObstacle;
    @BindView(R.id.gvBlock)
    NoScrollGridView gvBlock;

    int mSettingIsOK, mMarkIsOK, mHasObstacle, mHasBlock;
    String build_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_space);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        setViewStatus();
        getData();
        super.setDownloadPicOverInterface(this);
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("消防车道与消防扑救场地");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }

    public void setViewStatus() {
        rgSettingIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbSettingYes) {
                    mSettingIsOK = 1;
                } else {
                    mSettingIsOK = 0;
                }
            }
        });

        rgMarkIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbMarkYes) {
                    mMarkIsOK = 1;
                } else {
                    mMarkIsOK = 0;
                }
            }
        });

        rgHasObstacle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbObstacleYes) {
                    mHasObstacle = 1;
                } else {
                    mHasObstacle = 0;
                }
            }
        });

        rgHasBlock.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbBlockYes) {
                    mHasBlock = 1;
                } else {
                    mHasBlock = 0;
                }
            }
        });
    }


    public void getData() {
        HttpMethods.getInstance().getFireLanesInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireSpaceBean>(this) {
                    @Override
                    public void onNext(FireSpaceBean fireSpaceBean) {
                        if (fireSpaceBean == null || "".equals(fireSpaceBean.getGson_null())) {
                            fireSpaceBean = new FireSpaceBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            fireSpaceBean.setSize_up(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            fireSpaceBean.setIs_flag(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            fireSpaceBean.setIs_barrier(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            fireSpaceBean.setIs_occupy(bc4);
                            fireSpaceBean.setNo_edit(1);
                        }

                        fillData(fireSpaceBean);
                    }
                });
    }

    private void fillData(FireSpaceBean fireSpaceBean) {
        if (fireSpaceBean.getSize_up().getIs_ok() == 1) {
            rbSettingYes.setChecked(true);
        }

        etSettingProblem.setText(fireSpaceBean.getSize_up().getProblem());
        etSettingMeasure.setText(fireSpaceBean.getSize_up().getMeasures());

        if (fireSpaceBean.getIs_flag().getIs_ok() == 1) {
            rbMarkYes.setChecked(true);
        }

        etMarkProblem.setText(fireSpaceBean.getIs_flag().getProblem());
        etMarkMeasure.setText(fireSpaceBean.getIs_flag().getMeasures());

        if (fireSpaceBean.getIs_barrier().getIs_ok() == 1) {
            rbObstacleYes.setChecked(true);
        }

        etObstacleProblem.setText(fireSpaceBean.getIs_barrier().getProblem());
        etObstacleMeasure.setText(fireSpaceBean.getIs_barrier().getMeasures());

        if (fireSpaceBean.getIs_occupy().getIs_ok() == 1) {
            rbBlockYes.setChecked(true);
        }

        etBlockProblem.setText(fireSpaceBean.getIs_occupy().getProblem());
        etBlockMeasure.setText(fireSpaceBean.getIs_occupy().getMeasures());

        if (fireSpaceBean.getNo_edit() == 1) {
            tvSearch.setVisibility(View.VISIBLE);
        } else {
            tvSearch.setVisibility(View.INVISIBLE);
        }

        initGridViewList(fireSpaceBean);

    }

    private void doPost() {
        Map<String, String> a1 = new HashMap<>();
        a1.put("is_ok", mSettingIsOK + "");
        a1.put("problem", etSettingProblem.getText().toString());
        a1.put("measures", etSettingMeasure.getText().toString());

        Map<String, String> a2 = new HashMap<>();
        a2.put("is_ok", mMarkIsOK + "");
        a2.put("problem", etMarkProblem.getText().toString());
        a2.put("measures", etMarkMeasure.getText().toString());

        Map<String, String> a3 = new HashMap<>();
        a3.put("is_ok", mHasObstacle + "");
        a3.put("problem", etObstacleProblem.getText().toString());
        a3.put("measures", etObstacleMeasure.getText().toString());

        Map<String, String> a4 = new HashMap<>();
        a4.put("is_ok", mHasBlock + "");
        a4.put("problem", etBlockProblem.getText().toString());
        a4.put("measures", etBlockMeasure.getText().toString());

        Map<String, Map<String, String>> dataParams = new HashMap<>();
        dataParams.put("size_up", a1);
        dataParams.put("is_flag", a2);
        dataParams.put("is_barrier", a3);
        dataParams.put("is_occupy", a4);

        MultipartBody.Builder builder = new MultipartBody.Builder();

        putBody(builder, AppConfig.ACCESS_TOKEN, AppMediaType.TEXT, UserInfoUtil.getCurrentUserAccessToken());
        putBody(builder, "build_id", AppMediaType.TEXT, build_id);
        putBody(builder, "type", AppMediaType.TEXT, 1 + "");
        putBody(builder, "data", AppMediaType.TEXT, new Gson().toJson(dataParams));
        putBody(builder, "action_type", AppMediaType.TEXT, "fire_lane_site");


        // 图片数组
        File file;
        int size = mulGridViewBean.getSingleImageList().size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < mulGridViewBean.getSingleImageList().get(i).getImageList().size(); j++) {
                if (mulGridViewBean.getSingleImageList().get(i).getImageList().get(j).contains("http")) {
                    Log.e("ttt     ", mulGridViewBean.getSingleImageList().get(i).getImageList().get(j) + "\n");
                    continue;
                }
                else
                {
                    file = new File(mulGridViewBean.getSingleImageList().get(i).getImageList().get(j));
                    if (i ==0)
                    {
                        putBody(builder, "image*size_up[]", AppMediaType.JPG, file);
                    }
                    else if (i ==1)
                    {
                        putBody(builder, "image*is_flag[]", AppMediaType.JPG, file);
                    }
                    else if (i ==2)
                    {
                        putBody(builder, "image*is_barrier[]", AppMediaType.JPG, file);
                    }
                    else if (i ==3)
                    {
                        putBody(builder, "image*is_occupy[]", AppMediaType.JPG, file);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();

        HttpMethods.getInstance().postFireLanesInfo(multipartBody)
                .subscribe(new SampleProgressObserver<FireSpaceBean>(this) {
                    @Override
                    public void onNext(FireSpaceBean fireSpaceBean) {
                        ToastUtil.getInstances().showMsg("保存成功");
                    }
                });
    }

    void initGridViewList(FireSpaceBean fireSpaceBean) {
        List<SingleImageBean> singleImageBeans = new ArrayList<SingleImageBean>();

        SingleImageBean s1 = new SingleImageBean();
        s1.setPosition(0);
        s1.setDelModel(false);
        s1.setGridView(gvSetting);
        s1.setImageList(fireSpaceBean.getSize_up().getImage());
        singleImageBeans.add(s1);

        SingleImageBean s2 = new SingleImageBean();
        s2.setPosition(1);
        s2.setDelModel(false);
        s2.setGridView(gvMark);
        s2.setImageList(fireSpaceBean.getIs_flag().getImage());
        singleImageBeans.add(s2);

        SingleImageBean s3 = new SingleImageBean();
        s3.setPosition(2);
        s3.setDelModel(false);
        s3.setGridView(gvObstacle);
        s3.setImageList(fireSpaceBean.getIs_barrier().getImage());
        singleImageBeans.add(s3);

        SingleImageBean s4 = new SingleImageBean();
        s4.setPosition(3);
        s4.setDelModel(false);
        s4.setGridView(gvBlock);
        s4.setImageList(fireSpaceBean.getIs_occupy().getImage());
        singleImageBeans.add(s4);

        mulGridViewBean.setSingleImageList(singleImageBeans);
        refreshAdapter();
    }


    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvSearch:
                downloadImage();
                break;
            default:
                break;
        }
    }

    @Override
    public void post() {
        doPost();
    }
}
